package com.vegetable.modules.entity.finance.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * <p>
 * 客户逾期统计VO - 按客户ID统计各逾期等级的账期情况
 * </p>
 *
 * @author vegetable
 * @since 2025-10-25
 */
@Data
@ApiModel(value = "CustomerOverdueStatisticsVO", description = "客户逾期统计VO")
public class CustomerOverdueStatisticsVO implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "客户ID")
    private Long customerId;

    @ApiModelProperty(value = "客户名称")
    private String customerName;

    @ApiModelProperty(value = "预警级别：1轻微2严重3紧急")
    private Integer alertLevel;

    @ApiModelProperty(value = "预警天数")
    private Integer alertDays;

    @ApiModelProperty(value = "台账标识 0寿光 1河北 2聊城 3云南")
    private Integer regionId;

    @ApiModelProperty(value = "逾期订单总数")
    private Integer totalOrderCount;

    @ApiModelProperty(value = "未回款总金额")
    private BigDecimal totalUnreceivedAmount;

    @ApiModelProperty(value = "订单总金额")
    private BigDecimal totalOrderAmount;

    @ApiModelProperty(value = "已回款总金额")
    private BigDecimal totalReceivedAmount;

    @ApiModelProperty(value = "最大逾期天数")
    private Integer maxOverdueDays;

    @ApiModelProperty(value = "最小逾期天数")
    private Integer minOverdueDays;

    @ApiModelProperty(value = "平均逾期天数")
    private Integer avgOverdueDays;

    // ========== 各逾期等级统计 ==========

    @ApiModelProperty(value = "15天以内订单数")
    private Integer level1Count;

    @ApiModelProperty(value = "15天以内未回款金额")
    private BigDecimal level1UnreceivedAmount;

    @ApiModelProperty(value = "15-29天订单数")
    private Integer level2Count;

    @ApiModelProperty(value = "15-29天未回款金额")
    private BigDecimal level2UnreceivedAmount;

    @ApiModelProperty(value = "30-59天订单数")
    private Integer level3Count;

    @ApiModelProperty(value = "30-59天未回款金额")
    private BigDecimal level3UnreceivedAmount;

    @ApiModelProperty(value = "60-89天订单数")
    private Integer level4Count;

    @ApiModelProperty(value = "60-89天未回款金额")
    private BigDecimal level4UnreceivedAmount;

    @ApiModelProperty(value = "90天以上订单数")
    private Integer level5Count;

    @ApiModelProperty(value = "90天以上未回款金额")
    private BigDecimal level5UnreceivedAmount;

    /**
     * 获取预警级别名称
     */
    public String getAlertLevelName() {
        if (alertLevel == null) {
            return "";
        }
        switch (alertLevel) {
            case 1:
                return "轻微";
            case 2:
                return "严重";
            case 3:
                return "紧急";
            default:
                return "未知";
        }
    }

    /**
     * 获取台账名称
     */
    public String getRegionName() {
        if (regionId == null) {
            return "";
        }
        switch (regionId) {
            case 0:
                return "寿光";
            case 1:
                return "河北";
            case 2:
                return "聊城";
            case 3:
                return "云南";
            default:
                return "未知";
        }
    }

    /**
     * 获取风险等级（根据90天以上订单数判断）
     */
    public String getRiskLevel() {
        if (level5Count == null || level5Count == 0) {
            if (level4Count != null && level4Count > 0) {
                return "高";
            } else if (level3Count != null && level3Count > 0) {
                return "中";
            } else {
                return "低";
            }
        }
        if (level5Count >= 10) {
            return "极高";
        } else if (level5Count >= 5) {
            return "很高";
        } else {
            return "高";
        }
    }

    /**
     * 获取回款率
     */
    public BigDecimal getReceivedRate() {
        if (totalOrderAmount == null || totalOrderAmount.compareTo(BigDecimal.ZERO) == 0) {
            return BigDecimal.ZERO;
        }
        if (totalReceivedAmount == null) {
            return BigDecimal.ZERO;
        }
        return totalReceivedAmount.divide(totalOrderAmount, 4, BigDecimal.ROUND_HALF_UP)
                .multiply(new BigDecimal("100"));
    }
}
